home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7678 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  121 lines

  1. Path: isonews.bbn.hp.com!hpbblb!news
  2. From: Matthias Dittrich <matti>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Permutations
  5. Date: 26 Feb 1996 09:51:07 GMT
  6. Organization: Hewlett-Packard Co.
  7. Message-ID: <4grvqb$8n9@hpbblb.bbn.hp.com>
  8. References: <Dn8yz9.GGy@spuddy.mew.co.uk>
  9. NNTP-Posting-Host: trabant.bbn.hp.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.07 9000/712)
  14. X-URL: news:Dn8yz9.GGy@spuddy.mew.co.uk
  15.  
  16. david@spuddy.mew.co.uk (David Turner) wrote:
  17. >Does anyone know of an algorithm that works out the different purmutations
  18. >of ordering a number of objects? Not calculating how many there are, but 
  19. >actaully working out (quickly?) what they permutations are!
  20. >        eg. permutations of objects A, B and C:
  21. >                A B C   B A C   C A B
  22. >                A C B   B C A   C B A
  23. >Any help would be greatly appreciated.
  24. >David
  25. Here is one I've just written:
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29.  
  30. #define MAX_PERM 8
  31.  
  32. void permfunc(unsigned long i, char* str);
  33. void do_perm(char* str);
  34.  
  35. int main(int argc, char** argv)
  36. {
  37.   unsigned long n = 2, i;
  38.   char str[MAX_PERM];
  39.  
  40.     /* do some essential checks */
  41.   if(argc < 2) return 1;
  42.  
  43.   sscanf(argv[1], "%lu", &n);
  44.  
  45.   if(n >= MAX_PERM)
  46.   {
  47.     n = MAX_PERM - 1;
  48.     fprintf(stderr, "number too big, set to %d\n", n);
  49.   }
  50.  
  51.     /* initialize the string to show permutations */
  52.   for(i=0; i<n; i++)
  53.   {
  54.     str[i] = 'A' + i;
  55.   }
  56.   str[n] = '\0';
  57.  
  58.     /* call permfunc with starting level 0 */
  59.   permfunc(0, str);
  60.  
  61.   return 0;
  62. }
  63.  
  64. void permfunc(unsigned long i, char* str)
  65. {
  66.   char s[MAX_PERM];
  67.   char *t = s + i;
  68.   char *u = str + i;
  69.  
  70.     /* check if at least two characters are in the string */
  71.   if(*u && *(u + 1))
  72.   {
  73.     strcpy(s, str);
  74.  
  75.     permfunc(i + 1, s);
  76.     u = t + 1;
  77.  
  78.       /* the number of characters in the string is the number of
  79.          permutations to do */
  80.     while(*u)
  81.     {
  82.         /* after the permutation has been done
  83.            call this function recursively, increasing the starting level */
  84.       do_perm(t);
  85.       permfunc(i + 1, s);
  86.       u++;
  87.     }
  88.   }
  89.   else
  90.   {
  91.     printf("%s\n", str);
  92.   }
  93. }
  94.  
  95.   /* do the permutation on a string */
  96. void do_perm(char* str)
  97. {
  98.   char *s, *t, c;
  99.  
  100.   s = t = str;
  101.   t++;
  102.  
  103.     /* save first char */
  104.   c = *s;
  105.     /* move all chars one position to the left */
  106.   while(*t) *s++ = *t++;
  107.     /* set the last char */
  108.   *s = c;
  109. }
  110.  
  111. I think there may be faster ways to do it, but it works.
  112.  
  113. Good luck,
  114. Matthias
  115.  
  116.